package be.aquafin.klipdf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import be.aquafin.klipdf.model.SpatialNode;
import be.aquafin.klipdf.model.SpatialPipe;

import com.vividsolutions.jts.geom.Geometry;

/**
 * Databaseimplementatie SpatialDataDao
 * 
 * @author user2080
 *
 */
public class SpatialDataDaoImpl implements SpatialDataDao {
	
	@PersistenceContext(unitName = "jagisInPersistenceUnit")
	private EntityManager jagisInEntityMgr;

	private static final String EPSG = "31370";
	
	public List<SpatialPipe> getAllSpatialPipes() {
		return jagisInEntityMgr.createQuery("from SpatialPipe p", SpatialPipe.class).getResultList();
	}
	
	public List<SpatialNode> getAllSpatialNodes() {
		return jagisInEntityMgr.createQuery("from SpatialNode n", SpatialNode.class).getResultList();
	}

	public List<SpatialPipe> getSpatialPipesFromArea(Geometry geom) {
		String queryString = "from SpatialPipe p where GeoPipe.STIntersects(st_geom_from_Text('" + geom + "', " + EPSG + ")) = 1";
		return jagisInEntityMgr.createQuery(queryString, SpatialPipe.class).getResultList();
	}
	
	public List<SpatialNode> getSpatialNodesFromArea(Geometry geom) {
		String queryString = "from SpatialNode n where GeoNode.STIntersects(st_geom_from_Text('" + geom + "', " + EPSG + ")) = 1";
		return jagisInEntityMgr.createQuery(queryString, SpatialNode.class).getResultList();
	}

}
